package linkedhomework;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-01-05
 * Time: 16:55
 */
// 2、无头双向链表实现
public class LinkedList {
    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        linkedList.addLast(1);
        linkedList.addLast(2);
        linkedList.addLast(3);
        linkedList.addLast(4);
        linkedList.addLast(5);
        linkedList.display();
        System.out.println(linkedList.size());
        linkedList.addFirst(6);
        linkedList.display();
        linkedList.addIndex(2,7);
        linkedList.display();
        System.out.println(linkedList.contains(7));
        linkedList.remove(7);
        linkedList.display();
        linkedList.addFirst(8);
        linkedList.addLast(8);
        linkedList.display();
        linkedList.removeAllKey(8);
        linkedList.display();
        linkedList.clear();
        linkedList.display();
    }

    //设置内部静态类，作为结点
    static class Node {
        public int val;
        public Node pre;
        public Node next;

        public Node(int val) {
            this.val = val;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "val=" + val +
                    '}';
        }
    }

    public Node head;
    public Node rear;

    //头插法
    public void addFirst(int data) {
        final Node node = new Node(data);
        if (head == null) {
            head = node;
            rear = node;
        }else {
            node.next = head;
            head.pre = node;
            head = node;
        }
    }

    //尾插法
    public void addLast(int data) {
        final Node node = new Node(data);
        if (head == null) {
            addFirst(data);
        }else {
            rear.next = node;
            node.pre = rear;
            rear = node;
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            System.out.println("index位置不合法！");
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        Node node = new Node(data);
        Node cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        Node preNode = cur.pre;
        preNode.next = node;
        node.next = cur;
        node.pre = preNode;
        cur.pre = node;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        if (head == null) {
            throw new RuntimeException("链表为空！");
        }
        Node cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        Node cur = head;
        while (cur != null) {
            if(cur.val == key) {
                //判断当前是不是头节点
                if(cur == head) {
                    head = head.next;
                    if(head != null) {
                        head.pre = null;//这里有问题！！！！ 一个节点！
                    }
                }else {
                    //中间和尾巴的情况
                    cur.pre.next = cur.next;
                    if(cur.next != null) {
                        cur.next.pre = cur.pre;
                    }else {
                        rear = rear.pre;
                    }
                }
                return;//删完走人
            }else {
                cur = cur.next;
            }
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        Node cur = head;
        while (cur != null) {
            if(cur.val == key) {
                //判断当前是不是头节点
                if(cur == head) {
                    head = head.next;
                    if(head != null) {
                        head.pre = null;//这里有问题！！！！ 一个节点！
                    }
                }else {
                    //中间和尾巴的情况
                    cur.pre.next = cur.next;
                    if(cur.next != null) {
                        cur.next.pre = cur.pre;
                    }else {
                        rear = rear.pre;
                    }
                }
            }
            cur = cur.next;
        }
    }

    //得到单链表的长度
    public int size() {
        Node cur = head;
        int size = 0;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    public void display() {
        Node cur = head;
        while (cur != null) {
            System.out.println(cur);
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear() {
        Node cur = head;
        while (cur != null) {
            Node curNext = cur.next;
            cur.pre = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        rear = null;
    }
}
